<?php

define("NOFILTER", true);
define("APPNAME", substr(dirname($_SERVER['SCRIPT_NAME']), 1));
require dirname(dirname(__FILE__)) . '/core/loader.php';

$_menuconfig = array(
    array(
        '系统首页',
        array(
            'groupName' => '站点管理',
            'menulink' => array(
                array('站点列表.fa-th-list', 'yuming', 'index'),
            ),
        ),
    ),
    array(
        '内容管理',
        array(
            'groupName' => '栏目管理',
            'menulink' => array(
                array('栏目列表.fa-list-ul', 'classlist', 'index'),
            ),
        ),
        array(
            'groupName' => '文章管理',
            'menulink' => array(
                array('文章列表.fa-list-alt', 'article', 'index'),
                array('标签分类.fa-tag', 'tags', 'index'),
            ),
        ),
        array(
            'groupName' => '采集管理',
            'menulink' => array(
                array('采集管理.fa-bug', 'catch', 'index'),
            ),
        ),
    ),
    array(
        '搜索优化',
        array(
            'groupName' => '搜索优化',
            'menulink' => array(
                array('同义词替换.fa-exchange', 'seo', 'reword'),
                array('关键词优化.fa-file-word-o', 'seo', 'keyword'),
                array('关键词库.fa-file-word-o', 'seo', 'words'),
                array('手动推送.fa-exchange', 'bpush', 'index')
            ),
        ),
    ),
    array(
        '寄生虫管理',
        array(
            'groupName' => '寄生虫管理',
            'menulink' => array(
                array('词库管理.fa-exchange', 'jsc', 'index'),
                array('源文本管理.fa-file-word-o', 'jsc', 'content')
            ),
        ),
    ),
    array(
        '其他管理',
        array(
            'groupName' => '其他管理',
            'menulink' => array(
                array('爬虫统计.fa-pencil', 'spider', 'index'),
                array('友情链接.fa-link', 'flink', 'index'),
                array('广告管理', 'ads', 'index'),
            ),
        ),
    ),
    array(
        '系统设置',
        array(
            'groupName' => '系统设置',
            'menulink' => array(
                array('系统配置.fa-cog', 'system', 'config'),
                array('网站风格.fa-users', 'theme', 'index')
            ),
        ),
    ),
);

class admincp extends controller {

    public $user, $headMenu, $subMenu, $jnarrow;
    private $_menuconfig;
    public $allyuming = array();
    public $currentYuming;
    public $currentYumingId;

    public function __construct() {
        global $_menuconfig;
        parent::__construct();
        $this->user = admin::get();
        $this->_menuconfig = $_menuconfig;
        $this->loadHeaderMenu();
        $this->allyuming = db::select("select * from yuming order by id desc", 'id');

        $this->currentYumingId = gp("yuming_id") ? gp("yuming_id") : (!empty($_SESSION['yuming_id']) ? $_SESSION['yuming_id'] : '');
        if ($this->currentYumingId != '' && $this->currentYumingId != '-1' && isset($this->allyuming[$this->currentYumingId])) {
            $this->currentYuming = $this->allyuming[$this->currentYumingId];
            $_SESSION['yuming_id'] = $this->currentYumingId;
        } else {
            $this->currentYumingId = 0;
            $_SESSION['yuming_id'] = null;
        }

        $this->jnarrow = !empty($_COOKIE['jnarrow']);

        $this->loadLeftMenu();
    }

    /*
     *  顶部菜单
     */

    private function loadHeaderMenu() {
        foreach ($this->_menuconfig as $val) {
            $this->headMenu[] = array(
                'name' => $val[0],
                'id' => $val[1],
                'current' => 0,
            );
        }

        $findCur = false;
        foreach ($this->_menuconfig as $key => $val) {
            $val = array_slice($val, 1);
            foreach ($val as $vg) {
                foreach ($vg['menulink'] as $k => $v) {
                    if ($v[1] == M) {
                        $this->headMenu[$key]['current'] = 1;
                        $findCur = true;
                        break 3;
                    }
                }
            }
        }
        if (!$findCur) {
            $this->headMenu[0]['current'] = 1;
        }
    }

    /*
     *  左边菜单
     */

    public function loadLeftMenu() {
        $submenu = array();
        foreach ($this->_menuconfig as $key => $val) {
            $submenu_string = '';
            for ($i = 1; $i < count($val); $i++) {
                $submenu_string .= '<div>';
                $submenu_string .= '<div class="menu-title active"><i class="fa fa-caret-down fa-fw"></i>' . $val[$i]['groupName'] . '</div>';
                $submenu_string .= '<ul>';
                foreach ($val[$i]['menulink'] as $menu) {
                    $menu_arr = explode('.', $menu[0]);
                    $menu_class = isset($menu_arr[1]) ? $menu_arr[1] : 'fa-circle-o';
                    $submenu_string .= '<li><a href="?m=' . $menu[1] . '&a=' . $menu[2] . '"><span class="fa ' . $menu_class . ' fa-fw"></span>' . $menu_arr[0] . '</a></li>';
                }
                $submenu_string .= '</ul>';
                $submenu_string .= '</div>';
            }
            $submenu[$key] = $submenu_string;
        }
        $this->subMenu = json_encode($submenu);
    }

    public function respone_json($data = array(), $response = 200, $status = 0) {
        get_http_header($response);
        header('Content-type: application/json');
        $ret = array('status' => $status, 'result' => ($status == 1 ? array('url' => referer()) : array()));
        if (!(is_array($data))) {
            if (is_string($data)) {  //字符串 报错信息
                $ret['message'] = $data;
            } elseif ($data === 1) { //1表示操作成功无返回信息
                $ret['status'] = 1;
                $ret['message'] = '操作成功';
            }
        } elseif (is_array($data) && !empty($data)) {  //有数据的返回
            $ret['result'] = $data;
            $ret['status'] = 1;
        } else {
            $ret['result'] = $data;
        }
        ($ret);
        ob_clean();
        echo json_encode($ret);
        exit;
    }

}

loader::init()->load('session')->run();

